// ------------------------------------------------------------------------------------------
// Licensed by Interprise Solutions.
// http://www.InterpriseSolutions.com
// For details on this license please visit  the product homepage at the URL above.
// THE ABOVE NOTICE MUST REMAIN INTACT.
// ------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using InterpriseSuiteEcommerceCommon;
using InterpriseSuiteEcommerceControls.Validators;
using InterpriseSuiteEcommerceControls.Validators.Special;

namespace InterpriseSuiteEcommerceControls.mobile
{
    public class BillingAddressControl : AddressControl2
    {
        #region Variable Declaration

        private const string APPEARANCE_CATEGORY = "Address Appearance";
        private const string OK_TO_EMAIL_OPTIONS_GROUP = "OkToEmailGroup";

        private const string EMAIL_REQUIRED_ERROR_MESSAGE = "FirstNameRequired";
        private const string PASSWORD_REQUIRED_ERROR_MESSAGE = "PasswordRequired";
        private const string CONFIRM_PASSWORD_REQUIRED_ERROR_MESSAGE = "ConfirmPasswordRequired";

        private const string EMAIL_MAXIMUM_CHARACTER_LENGTH = "EmailMaximumCharacterLength";
        private const string PASSWORD_MINIMUM_CHARACTER_LENGTH = "PasswordMinimumCharacterLength";
        private const string PASSWORD_MAXIMUM_CHARACTER_LENGTH = "PasswordMaximumCharacterLength";

        private const string EMAIL_MAXIMUM_CHARACTER_LENGTH_ERROR_MESSAGE = "EmailMaximumCharacterLengthErrorMessage";
        private const string PASSWORD_MAXIMUM_CHARACTER_LENGTH_ERROR_MESSAGE = "PasswordMaximumCharacterLengthErrorMessage";

        private const string EMAIL_VALIDATION_REGULAR_EXPRESSION = "EmailValidationRegularExpression";
        private const string EMAIL_VALIDATION_REGULAR_EXPRESSION_ERROR_MESSAGE = "EmailValidationRegularExpressionErrorMessage";

        private const string COMPARE_CONFIRM_PASSWORD_ERROR_MESSAGE = "ConfirmPasswordErrorMessage";

        private const string REQUIRE_CAPTCHA = "RequireCaptcha";

        private const string CAPTCHA_REQUIRED_ERROR_MESSAGE = "CaptchaRequiredErrorMessage";
        private const string CAPTCHA_INVALID_ERROR_MESSAGE = "CaptchaInvalidErrorMessage";
        private const string CAPTCHA_RANDOM_LENGTH = "CaptchaRandomLength";

        private const int DEFAULT_CAPTCHA_LENGTH = 6;

        private const string REQUIRE_OVER_13 = "RequireOver13";
        private const string REQUIRE_SAVE_CC_DETAILS = "RequireSaveCCDetails";

        private const string REQUIRE_STRONG_PASSWROD = "RequireStrongPassword";
        private const string STRONG_PASSWORD_VALIDATION_REGULAR_EXPRESSION = "StrongPasswordValidationRegularExpression";
        private const string STRONG_PASSWORD_VALIDATION_EXPRESSIOn_ERROR_MESSAGE = "StrongPasswordValidationRegularExpressionErrorMessage";

        private const string REQUIRE_NO_DUPLICATE_CUSTOMER_EMAIL = "RequireNoDuplicateCustomerEmail";
        private const string REQUIRE_NO_DUPLICATE_CUSTOMER_EMAIL_ERROR_MESSAGE = "RequireNoDuplicateCustomerEmailErrorMessage";

        private const string REQUIRE_BUSINESS_TYPE = "RequireBusinessType";
        private const string REQUIRE_BUSINESS_TYPE_SELECT_ERROR_MESSAGE = "RequireBusinessTypeSelect";

        private const string BUSINESS_TYPE_RETAIL_DISPLAY_TEXT = "BusinessTypeRetailDisplayText";
        private const string BUSINESS_TYPE_WHOLESALE_DISPLAY_TEXT = "BusinessTypeWholeSaleDisplayText";


        private Label _lblSalutationCaption;
        private DropDownList _cboSalutation;

        private Label _lblEmailCaption;
        private TextBox _txtEmail;

        private Label _lblPasswordCaption;
        private TextBox _txtPassword;
        private Label _lblPasswordMinLengthCaption = null;

        private Label _lblConfirmPassword;
        private TextBox _txtConfirmPassword;

        private Label _lblOkToEmailCaption;
        private RadioButton _rbOkToEmailYesOption;
        private RadioButton _rbOkToEmailNoOption;
        private Label _lblSubscriptionOptionCaption;

        private Label _lblOver13YearsOldCaption;
        private CheckBox _chkOver13YearsOld;

        private Label _lblCaptchaCaption;
        private TextBox _txtCaptcha;
        private Image _imgCaptcha;

        private Label _lblBusinessType;
        private DropDownList _cboBusinessType;

        private TableRow _rowTaxNUmber;
        private Label _lblTaxNumber;
        private TextBox _txtTaxNumber;

        private CheckBox _chkSaveCCDetails;
        private Label _lblSaveCCDetails;

        private const string REQUIRE_SALUTATION = "RequireSalutation";
        private const string REQUIRE_EMAIL = "RequireEmail";
        private const string REQUIRE_PASSWORD = "RequirePassword";
        private const string REQUIRE_OK_TO_EMAIL = "RequireOkToEmail";

        #endregion

        public event CancelEventHandler EvaluateNoDuplicateCustomerEmail;

        #region Constructor

        public BillingAddressControl()
            : base()
        {
            _lblSalutationCaption = new Label();
            _cboSalutation = new DropDownList();

            _lblEmailCaption = new Label();
            _txtEmail = new TextBox();

            _lblPasswordCaption = new Label();
            _txtPassword = new TextBox();
            _txtPassword.TextMode = TextBoxMode.Password;
            _lblPasswordMinLengthCaption = new Label();

            _lblConfirmPassword = new Label();
            _txtConfirmPassword = new TextBox();
            _txtConfirmPassword.TextMode = TextBoxMode.Password;

            _lblOkToEmailCaption = new Label();
            _rbOkToEmailYesOption = new RadioButton();
            _rbOkToEmailNoOption = new RadioButton();
            _rbOkToEmailYesOption.GroupName = OK_TO_EMAIL_OPTIONS_GROUP;
            _rbOkToEmailNoOption.GroupName = OK_TO_EMAIL_OPTIONS_GROUP;
            _lblSubscriptionOptionCaption = new Label();

            _lblOver13YearsOldCaption = new Label();
            _chkOver13YearsOld = new CheckBox();

            _lblCaptchaCaption = new Label();
            _txtCaptcha = new TextBox();
            _imgCaptcha = new Image();

            _chkSaveCCDetails = new CheckBox();
            _lblSaveCCDetails = new Label();

            _lblBusinessType = new Label();
            _cboBusinessType = new DropDownList();
            _cboBusinessType.Items.Clear();
            _cboBusinessType.Items.Add(new ListItem("Choose Business Type", string.Empty));
            _cboBusinessType.Items.Add(new ListItem(this.BusinessTypeRetailDisplayText, Customer.BusinessTypes.Retail.ToString()));
            _cboBusinessType.Items.Add(new ListItem(this.BusinessTypeWholeSaleDisplayText, Customer.BusinessTypes.WholeSale.ToString()));

            _rowTaxNUmber = new TableRow();
            _lblTaxNumber = new Label();
            _txtTaxNumber = new TextBox();

            SetDefaults();

            base.RowAdding += new EventHandler<RowEventArgs>(AccountAddressControl_RowAdding);
            base.RowAdded += new EventHandler<RowEventArgs>(AccountAddressControl_RowAdded);
            base.PreRender += new EventHandler(BillingAddressControl_PreRender);
        }

        private void BillingAddressControl_PreRender(object sender, EventArgs e)
        {
            ClearSecurityCode();
        }

        private void ClearSecurityCode()
        {
            _txtCaptcha.Text = string.Empty;
        }

        #endregion

        private void SetDefaults()
        {
            SetDefaultVisibilities();
            AssignClientReferenceIds();
            SetDefaultCaptions();
        }

        private void SetDefaultVisibilities()
        {
            this.RequireEmail = true;
            this.RequireSalutation = true;
            this.RequireStrongPassword = false;
            this.RequireOver13 = true;
            this.RequireOkToEmail = true;
            this.RequirePassword = true;
        }

        private void AssignClientReferenceIds()
        {
            _cboSalutation.ID = "Salutation";
            _txtEmail.ID = "Email";
            _txtPassword.ID = "Password";
            _txtConfirmPassword.ID = "ConfirmPassword";
            _rbOkToEmailYesOption.ID = "OkToEmailYes";
            _rbOkToEmailNoOption.ID = "OkToEmailNo";
            _chkOver13YearsOld.ID = "Over13";
            _txtCaptcha.ID = "Captcha";
            _cboBusinessType.ID = "BusinessType";
            _txtTaxNumber.ID = "TaxNumber";
            _rowTaxNUmber.ID = "TaxNumberRow";
            _chkSaveCCDetails.ID = "SaveCCDetalils";
        }

        private void SetDefaultCaptions()
        {
            this.SalutationCaption = "Salutation";
            this.EmailCaption = "Email";
            this.OkToEmailCaption = "Ok to email";
            this.Over13YearsOldCaption = "Over 13 Years Old";
            this.PasswordCaption = "Password";
            this.ConfirmPasswordCaption = "Confirm Password";
            this.CaptchaCaption = "Enter Security Number";
        }

        [Browsable(true), Category(APPEARANCE_CATEGORY)]
        public string BusinessTypeCaption
        {
            get { return _lblBusinessType.Text; }
            set { _lblBusinessType.Text = value; }
        }

        [Browsable(true), Category(APPEARANCE_CATEGORY)]
        public string TaxNumberCaption
        {
            get { return _lblTaxNumber.Text; }
            set { 
                _lblTaxNumber.Text = value;

                if (_txtTaxNumber != null) _txtTaxNumber.Attributes.Add("placeholder", value);
            }
        }

        [Browsable(true), Category(APPEARANCE_CATEGORY)]
        public bool RequireBusinessType
        {
            get
            {
                object savedValue = ViewState[REQUIRE_BUSINESS_TYPE];
                return null != savedValue && savedValue is bool && (bool)savedValue;
            }
            set
            {
                ViewState[REQUIRE_BUSINESS_TYPE] = value;
            }
        }

        [Browsable(true), Category(APPEARANCE_CATEGORY)]
        public string RequireBusinessTypeSelectErrorMessage
        {
            get
            {
                object savedValue = ViewState[REQUIRE_BUSINESS_TYPE_SELECT_ERROR_MESSAGE];
                if (null == savedValue) { return string.Empty; }

                return savedValue.ToString();
            }
            set
            {
                ViewState[REQUIRE_BUSINESS_TYPE_SELECT_ERROR_MESSAGE] = value;
            }
        }

        private const string CHOOSE_BUSINESS_TYPE_DISPLAY_TEXT = "ChooseBusinesstypeDisplayText";

        [Browsable(true), Category(APPEARANCE_CATEGORY)]
        public string ChooseBusinessTypeDisplayText
        {
            get
            {
                object savedValue = ViewState[CHOOSE_BUSINESS_TYPE_DISPLAY_TEXT];
                if (null == savedValue) { return string.Empty; }

                return savedValue.ToString();
            }
            set
            {
                ViewState[CHOOSE_BUSINESS_TYPE_DISPLAY_TEXT] = value;

                _cboBusinessType.Items[0].Text = value;
            }
        }

        [Browsable(true), Category(APPEARANCE_CATEGORY)]
        public string BusinessTypeRetailDisplayText
        {
            get
            {
                object savedValue = ViewState[BUSINESS_TYPE_RETAIL_DISPLAY_TEXT];
                if (null == savedValue) { return string.Empty; }

                return savedValue.ToString();
            }
            set
            {
                ViewState[BUSINESS_TYPE_RETAIL_DISPLAY_TEXT] = value;

                _cboBusinessType.Items[1].Text = value;
            }
        }

        [Browsable(true), Category(APPEARANCE_CATEGORY)]
        public string BusinessTypeWholeSaleDisplayText
        {
            get
            {
                object savedValue = ViewState[BUSINESS_TYPE_WHOLESALE_DISPLAY_TEXT];
                if (null == savedValue) { return string.Empty; }

                return savedValue.ToString();
            }
            set
            {
                ViewState[BUSINESS_TYPE_WHOLESALE_DISPLAY_TEXT] = value;

                _cboBusinessType.Items[2].Text = value;
            }
        }

        [Browsable(true), Category(APPEARANCE_CATEGORY)]
        public bool RequireSalutation
        {
            get
            {
                object savedValue = ViewState[REQUIRE_SALUTATION];
                return null != savedValue && savedValue is bool && (bool)savedValue;
            }
            set
            {
                ViewState[REQUIRE_SALUTATION] = value;
            }
        }

        [Browsable(true), Category(APPEARANCE_CATEGORY)]
        public bool RequireEmail
        {
            get
            {
                object savedValue = ViewState[REQUIRE_EMAIL];
                return null != savedValue && savedValue is bool && (bool)savedValue;
            }
            set
            {
                ViewState[REQUIRE_EMAIL] = value;
            }
        }

        [Browsable(true), Category(APPEARANCE_CATEGORY)]
        public bool RequireNoDuplicateCustomerEmail
        {
            get
            {
                object savedValue = ViewState[REQUIRE_NO_DUPLICATE_CUSTOMER_EMAIL];
                return null != savedValue && savedValue is bool && (bool)savedValue;
            }
            set
            {
                ViewState[REQUIRE_NO_DUPLICATE_CUSTOMER_EMAIL] = value;
            }
        }

        [Browsable(true), Category(APPEARANCE_CATEGORY)]
        public bool RequirePassword
        {
            get
            {
                object savedValue = ViewState[REQUIRE_PASSWORD];
                return null != savedValue && savedValue is bool && (bool)savedValue;
            }
            set
            {
                ViewState[REQUIRE_PASSWORD] = value;
            }
        }

        [Browsable(true), Category(APPEARANCE_CATEGORY)]
        public bool RequireOkToEmail
        {
            get
            {
                object savedValue = ViewState[REQUIRE_OK_TO_EMAIL];
                return null != savedValue && savedValue is bool && (bool)savedValue;
            }
            set
            {
                ViewState[REQUIRE_OK_TO_EMAIL] = value;
            }
        }

        [Browsable(true), Category(APPEARANCE_CATEGORY)]
        public bool RequireCaptcha
        {
            get
            {
                object savedValue = ViewState[REQUIRE_CAPTCHA];
                return null != savedValue && savedValue is bool && (bool)savedValue;
            }
            set
            {
                ViewState[REQUIRE_CAPTCHA] = value;
            }
        }

        [Browsable(true), Category(APPEARANCE_CATEGORY)]
        public bool RequireOver13
        {
            get
            {
                object savedValue = ViewState[REQUIRE_OVER_13];
                return null != savedValue && savedValue is bool && (bool)savedValue;
            }
            set
            {
                ViewState[REQUIRE_OVER_13] = value;
            }
        }

        [Browsable(true), Category(APPEARANCE_CATEGORY)]
        public bool RequireSaveCCDetails
        {
            get
            {
                object savedValue = ViewState[REQUIRE_SAVE_CC_DETAILS];
                return null != savedValue && savedValue is bool && (bool)savedValue;
            }
            set
            {
                ViewState[REQUIRE_SAVE_CC_DETAILS] = value;
            }
        }

        [Browsable(true)]
        public string CaptchaImageUrl
        {
            get
            {
                return _imgCaptcha.ImageUrl;
            }
            set
            {
                _imgCaptcha.ImageUrl = value;
            }
        }

        [Browsable(true), Category(VALIDATORS_CATEGORY)]
        public int CaptchaLength
        {
            get
            {
                object savedValue = ViewState[CAPTCHA_RANDOM_LENGTH];

                if (null == savedValue || !(savedValue is int))
                {
                    return DEFAULT_CAPTCHA_LENGTH;
                }

                return (int)savedValue;
            }
            set
            {
                ViewState[CAPTCHA_RANDOM_LENGTH] = value;
            }
        }

        public bool SaveCCDetails
        {
            get { return _chkSaveCCDetails.Checked; }
            set { _chkSaveCCDetails.Checked = value; }
        }

        public string SaveCCDetailsCaption
        {
            get { return _lblSaveCCDetails.Text; }
            set { _lblSaveCCDetails.Text = value; }
        }

        [Browsable(true)]
        public string CaptchaCaption
        {
            get { return _lblCaptchaCaption.Text; }
            set { _lblCaptchaCaption.Text = value; }
        }

        public string Captcha
        {
            get { return _txtCaptcha.Text; }
            set { _txtCaptcha.Text = value; }
        }

        [Browsable(true), Category(VALIDATORS_CATEGORY)]
        public string CaptchaRequiredErrorMessage
        {
            get
            {
                object savedValue = ViewState[CAPTCHA_REQUIRED_ERROR_MESSAGE];
                if (null == savedValue) { return string.Empty; }

                return savedValue.ToString();
            }
            set
            {
                ViewState[CAPTCHA_REQUIRED_ERROR_MESSAGE] = value;
            }
        }

        [Browsable(true), Category(VALIDATORS_CATEGORY)]
        public string CaptchaInvalidErrorMessage
        {
            get
            {
                object savedValue = ViewState[CAPTCHA_INVALID_ERROR_MESSAGE];
                if (null == savedValue) { return string.Empty; }

                return savedValue.ToString();
            }
            set
            {
                ViewState[CAPTCHA_INVALID_ERROR_MESSAGE] = value;
            }
        }

        [Browsable(true), Category(VALIDATORS_CATEGORY)]
        public string EmailRequiredErrorMessage
        {
            get
            {
                object savedValue = ViewState[EMAIL_REQUIRED_ERROR_MESSAGE];
                if (null == savedValue) { return string.Empty; }

                return savedValue.ToString();
            }
            set
            {
                ViewState[EMAIL_REQUIRED_ERROR_MESSAGE] = value;
            }
        }

        [Browsable(true), Category(VALIDATORS_CATEGORY)]
        public string RequireNoDuplicateCustomerEmailErrorMessage
        {
            get
            {
                object savedValue = ViewState[REQUIRE_NO_DUPLICATE_CUSTOMER_EMAIL_ERROR_MESSAGE];
                if (null == savedValue) { return string.Empty; }

                return savedValue.ToString();
            }
            set
            {
                ViewState[REQUIRE_NO_DUPLICATE_CUSTOMER_EMAIL_ERROR_MESSAGE] = value;
            }
        }

        [Browsable(true), Category(VALIDATORS_CATEGORY)]
        public string PasswordRequiredErrorMessage
        {
            get
            {
                object savedValue = ViewState[PASSWORD_REQUIRED_ERROR_MESSAGE];
                if (null == savedValue) { return string.Empty; }

                return savedValue.ToString();
            }
            set
            {
                ViewState[PASSWORD_REQUIRED_ERROR_MESSAGE] = value;
            }
        }

        [Browsable(true), Category(VALIDATORS_CATEGORY)]
        public string ConfirmPasswordRequiredErrorMessage
        {
            get
            {
                object savedValue = ViewState[CONFIRM_PASSWORD_REQUIRED_ERROR_MESSAGE];
                if (null == savedValue) { return string.Empty; }

                return savedValue.ToString();
            }
            set
            {
                ViewState[CONFIRM_PASSWORD_REQUIRED_ERROR_MESSAGE] = value;
            }
        }

        [Browsable(true), Category(VALIDATORS_CATEGORY)]
        public int EmailMaximumCharacterLength
        {
            get
            {
                object savedValue = ViewState[EMAIL_MAXIMUM_CHARACTER_LENGTH];

                if (null == savedValue || !(savedValue is int))
                {
                    return InputLengthValidator.NoMaxLengthFilter;
                }

                return (int)savedValue;
            }
            set
            {
                ViewState[EMAIL_MAXIMUM_CHARACTER_LENGTH] = value;
            }
        }

        [Browsable(true), Category(VALIDATORS_CATEGORY)]
        public int PasswordMinimumCharacterLength
        {
            get
            {
                object savedValue = ViewState[PASSWORD_MINIMUM_CHARACTER_LENGTH];

                if (null == savedValue || !(savedValue is int))
                {
                    return InputLengthValidator.NoMinLengthFilter;
                }

                return (int)savedValue;
            }
            set
            {
                ViewState[PASSWORD_MINIMUM_CHARACTER_LENGTH] = value;
            }
        }

        [Browsable(true), Category(VALIDATORS_CATEGORY)]
        public int PasswordMaximumCharacterLength
        {
            get
            {
                object savedValue = ViewState[PASSWORD_MAXIMUM_CHARACTER_LENGTH];

                if (null == savedValue || !(savedValue is int))
                {
                    return InputLengthValidator.NoMaxLengthFilter;
                }

                return (int)savedValue;
            }
            set
            {
                ViewState[PASSWORD_MAXIMUM_CHARACTER_LENGTH] = value;
            }
        }

        [Browsable(true), Category(VALIDATORS_CATEGORY)]
        public string EmailMaximumCharacterLengthErrorMessage
        {
            get
            {
                object savedValue = ViewState[EMAIL_MAXIMUM_CHARACTER_LENGTH_ERROR_MESSAGE];
                if (null == savedValue) { return string.Empty; }

                return savedValue.ToString();
            }
            set
            {
                ViewState[EMAIL_MAXIMUM_CHARACTER_LENGTH_ERROR_MESSAGE] = value;
            }
        }

        [Browsable(true), Category(VALIDATORS_CATEGORY)]
        public string PasswordMaximumCharacterLengthErrorMessage
        {
            get
            {
                object savedValue = ViewState[PASSWORD_MAXIMUM_CHARACTER_LENGTH_ERROR_MESSAGE];
                if (null == savedValue) { return string.Empty; }

                return savedValue.ToString();
            }
            set
            {
                ViewState[PASSWORD_MAXIMUM_CHARACTER_LENGTH_ERROR_MESSAGE] = value;
            }
        }

        [Browsable(true), Category(VALIDATORS_CATEGORY)]
        public string EmailValidationRegularExpression
        {
            get
            {
                object savedValue = ViewState[EMAIL_VALIDATION_REGULAR_EXPRESSION];
                if (null == savedValue) { return string.Empty; }

                return savedValue.ToString();
            }
            set
            {
                ViewState[EMAIL_VALIDATION_REGULAR_EXPRESSION] = value;
            }
        }

        [Browsable(true), Category(VALIDATORS_CATEGORY)]
        public string EmailValidationRegularExpressionErrorMessage
        {
            get
            {
                object savedValue = ViewState[EMAIL_VALIDATION_REGULAR_EXPRESSION_ERROR_MESSAGE];
                if (null == savedValue) { return string.Empty; }

                return savedValue.ToString();
            }
            set
            {
                ViewState[EMAIL_VALIDATION_REGULAR_EXPRESSION_ERROR_MESSAGE] = value;
            }
        }

        [Browsable(true), Category(VALIDATORS_CATEGORY)]
        public string CompareConfirmPasswordErrorMessage
        {
            get
            {
                object savedValue = ViewState[COMPARE_CONFIRM_PASSWORD_ERROR_MESSAGE];
                if (null == savedValue) { return string.Empty; }

                return savedValue.ToString();
            }
            set
            {
                ViewState[COMPARE_CONFIRM_PASSWORD_ERROR_MESSAGE] = value;
            }
        }

        [Browsable(true), Category(APPEARANCE_CATEGORY)]
        public bool RequireStrongPassword
        {
            get
            {
                object savedValue = ViewState[REQUIRE_STRONG_PASSWROD];
                return null != savedValue && savedValue is bool && (bool)savedValue;
            }
            set
            {
                ViewState[REQUIRE_STRONG_PASSWROD] = value;
            }
        }

        [Browsable(true), Category(VALIDATORS_CATEGORY)]
        public string StrongPasswordValidationRegularExpression
        {
            get
            {
                object savedValue = ViewState[STRONG_PASSWORD_VALIDATION_REGULAR_EXPRESSION];
                if (null == savedValue) { return string.Empty; }

                return savedValue.ToString();
            }
            set
            {
                ViewState[STRONG_PASSWORD_VALIDATION_REGULAR_EXPRESSION] = value;
            }
        }

        [Browsable(true), Category(VALIDATORS_CATEGORY)]
        public string StrongPasswordValidationErrorMessage
        {
            get
            {
                object savedValue = ViewState[STRONG_PASSWORD_VALIDATION_EXPRESSIOn_ERROR_MESSAGE];
                if (null == savedValue) { return string.Empty; }

                return savedValue.ToString();
            }
            set
            {
                ViewState[STRONG_PASSWORD_VALIDATION_EXPRESSIOn_ERROR_MESSAGE] = value;
            }
        }

        protected override List<InputValidator> ProvideValidators()
        {

            List<InputValidator> validators = base.ProvideValidators();

            if (this.RequireEmail)
            {
                InputValidator validateNoDuplicateEmail = null;
                if (this.RequireNoDuplicateCustomerEmail)
                {
                    validateNoDuplicateEmail =
                    new NoDuplicateCustomerEmailValidator(_txtEmail, this.RequireNoDuplicateCustomerEmailErrorMessage);

                    validateNoDuplicateEmail.Evaluate += new CancelEventHandler(OnEvaluateNoDuplicateCustomerEmail);
                }

                validators.Add(
                    MakeRequiredInputValidator(
                        _txtEmail,
                        this.EmailRequiredErrorMessage,
                        MakeInputLengthValidator(
                            _txtEmail,
                            InputLengthValidator.NoMinLengthFilter,
                            this.EmailMaximumCharacterLength,
                            this.EmailMaximumCharacterLengthErrorMessage,
                            MakeRegularExpressionInputValidator(
                                _txtEmail,
                                this.EmailValidationRegularExpression,
                                this.EmailValidationRegularExpressionErrorMessage,
                                validateNoDuplicateEmail
                            )
                        )
                    )
                );

            }

            if (this.RequirePassword)
            {
                RegularExpressionInputValidator strongPasswordValidator = null;

                // ensure chaining...
                if (this.RequireStrongPassword)
                {
                    strongPasswordValidator = MakeRegularExpressionInputValidator(_txtPassword, this.StrongPasswordValidationRegularExpression, this.StrongPasswordValidationErrorMessage);
                }

                CompareInputValidator confirmPasswordValidator = MakeCompareInputValidator(_txtConfirmPassword, _txtPassword, this.CompareConfirmPasswordErrorMessage, strongPasswordValidator);

                validators.Add(
                    MakeRequiredInputValidator(
                        _txtPassword,
                        this.PasswordRequiredErrorMessage,
                        MakeInputLengthValidator(
                            _txtPassword,
                            this.PasswordMinimumCharacterLength,
                            this.PasswordMaximumCharacterLength,
                            this.PasswordMaximumCharacterLengthErrorMessage,
                            confirmPasswordValidator
                        )
                    )
                );
            }

            if (this.RequireCaptcha)
            {
                CaptchaInputValidator captchaValidator = new CaptchaInputValidator(_txtCaptcha, this.CaptchaInvalidErrorMessage);
                base.HandleValidationErrorEvent(captchaValidator);

                validators.Add(
                    MakeRequiredInputValidator(_txtCaptcha, this.CaptchaRequiredErrorMessage, captchaValidator)
                );

            }

            if (this.RequireBusinessType)
            {
                DropDownValidator businessTypeValidator = new DropDownValidator(_cboBusinessType, 0, this.RequireBusinessTypeSelectErrorMessage);
                base.HandleValidationErrorEvent(businessTypeValidator);

                validators.Add(businessTypeValidator);
            }

            return validators;
        }

        #region Properties

        [Browsable(true), Category(APPEARANCE_CATEGORY)]
        public string SalutationCaption
        {
            get { return _lblSalutationCaption.Text; }
            set { _lblSalutationCaption.Text = value; }
        }

        [Browsable(true), Category(APPEARANCE_CATEGORY)]
        public string EmailCaption
        {
            get { return _lblEmailCaption.Text; }
            set 
            { 
                _lblEmailCaption.Text = value;
                _txtEmail.Attributes.Add("placeholder", value);
            }
        }

        [Browsable(true), Category(APPEARANCE_CATEGORY)]
        public string PasswordCaption
        {
            get { return _lblPasswordCaption.Text; }
            set 
            { 
                _lblPasswordCaption.Text = value;
                _txtPassword.Attributes.Add("placeholder", value);
            }
        }

        [Browsable(true), Category(APPEARANCE_CATEGORY)]
        public string PasswordMinLengthCaption
        {
            get { return _lblPasswordMinLengthCaption.Text; }
            set { _lblPasswordMinLengthCaption.Text = value; }
        }

        [Browsable(true), Category(APPEARANCE_CATEGORY)]
        public string ConfirmPasswordCaption
        {
            get { return _lblConfirmPassword.Text; }
            set 
            { 
                _lblConfirmPassword.Text = value;
                _txtConfirmPassword.Attributes.Add("placeholder", value);
            }
        }

        [Browsable(true), Category(APPEARANCE_CATEGORY)]
        public string OkToEmailCaption
        {
            get { return _lblOkToEmailCaption.Text; }
            set 
            { 
                _lblOkToEmailCaption.Text = value; 
            }
        }

        [Browsable(true), Category(APPEARANCE_CATEGORY)]
        public string OkToEmailYesOptionCaption
        {
            get { return _rbOkToEmailYesOption.Text; }
            set { _rbOkToEmailYesOption.Text = value; }
        }

        [Browsable(true), Category(APPEARANCE_CATEGORY)]
        public string OkToEmailNoOptionCaption
        {
            get { return _rbOkToEmailNoOption.Text; }
            set { _rbOkToEmailNoOption.Text = value; }
        }

        [Browsable(true), Category(APPEARANCE_CATEGORY)]
        public string SubScriptionOptionCaption
        {
            get { return _lblSubscriptionOptionCaption.Text; }
            set { _lblSubscriptionOptionCaption.Text = value; }
        }

        [Browsable(true), Category(APPEARANCE_CATEGORY)]
        public string Over13YearsOldCaption
        {
            get { return _lblOver13YearsOldCaption.Text; }
            set { _lblOver13YearsOldCaption.Text = value; }
        }

        [Browsable(true)]
        public string TaxNumber
        {
            get { return _txtTaxNumber.Text; }
            set 
            { 
                _txtTaxNumber.Text = value;
            }
        }

        public string Salutation
        {
            get
            {
                if (null == _cboSalutation.SelectedItem) { return string.Empty; }
                return _cboSalutation.SelectedItem.Value;
            }
            set
            {
                try
                {
                    _cboSalutation.SelectedValue = value;
                }
                catch
                {
                    _cboSalutation.SelectedValue = null;
                }
            }
        }

        public string Email
        {
            get { return _txtEmail.Text; }
            set 
            { 
                _txtEmail.Text = value;
            }
        }

        public string Password
        {
            get { return _txtPassword.Text; }
            set
            {
                // This assumes that the user of this control
                // knows what he is doing..

                // Setting the password through the text
                // will not display at runtime
                // therefore we will resort to using the value attribute
                //_txtPassword.Text = value; <- won't work by design
                _txtPassword.Attributes["value"] = value;
                _txtPassword.Attributes.Add("placeholder", value);
            }
        }

        public string ConfirmedPassword
        {
            get { return _txtConfirmPassword.Text; }
            set
            {
                _txtConfirmPassword.Attributes["value"] = value;
            }
        }

        public bool OkToEmailChecked
        {
            get { return _rbOkToEmailYesOption.Checked; }
            set
            {
                _rbOkToEmailYesOption.Checked = value;
                _rbOkToEmailNoOption.Checked = !value;
            }
        }

        public bool Over13Checked
        {
            get { return _chkOver13YearsOld.Checked; }
            set { _chkOver13YearsOld.Checked = value; }
        }

        #endregion

        #region Methods

        public void SetSalutations(List<KeyValuePair<string, string>> salutations)
        {
            foreach (KeyValuePair<string, string> salutation in salutations)
            {
                _cboSalutation.Items.Add(new ListItem(salutation.Key, salutation.Value));
            }
        }

        public override void SetAddressDisplay(Address displayAddress)
        {
            base.SetAddressDisplay(displayAddress);

            this.Email = displayAddress.EMail;
        }

        public Address ExtractAddress(Customer withAssociatedCustomer)
        {
            return ExtractAddress(withAssociatedCustomer, AddressTypes.Billing);
        }

        public override Address ExtractAddress(Customer withAssociatedCustomer, AddressTypes usingType)
        {
            Address thisAddress = base.ExtractAddress(withAssociatedCustomer, usingType);
            withAssociatedCustomer.Password = this.Password;
            withAssociatedCustomer.EMail = this.Email;
            withAssociatedCustomer.IsOver13 = this.Over13Checked;
            withAssociatedCustomer.IsOKToEMail = this.OkToEmailChecked;
            withAssociatedCustomer.FirstName = this.FirstName;
            withAssociatedCustomer.LastName = this.LastName;

            if (this.RequireBusinessType)
            {
                withAssociatedCustomer.BusinessType = this.BusinessType;
                withAssociatedCustomer.TaxNumber = this.TaxNumber;
            }

            withAssociatedCustomer.Salutation = this.Salutation;
            thisAddress.EMail = this.Email;

            return thisAddress;
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
        }

        #endregion

        #region Event Handlers

        private void AccountAddressControl_RowAdding(object sender, AddressControl2.RowEventArgs e)
        {
            if (e.Row == AddressRow.FirstName)
            {
                if (this.RequireSalutation)
                {
                    //AddFieldRow(_lblSalutationCaption);
                    AddFieldRow(null, _cboSalutation);
                }
            }
        }

        private void AccountAddressControl_RowAdded(object sender, AddressControl2.RowEventArgs e)
        {
            if (e.Row == AddressRow.CityStatePostal && this.RequireBusinessType)
            {
                //AddFieldRow(_lblBusinessType);
                AddFieldRow(null, _cboBusinessType);
                AddFieldRow(_rowTaxNUmber, null , null);
                AddFieldRow(null, _txtTaxNumber);
            }

            if (e.Row == AddressRow.Phone)
            {
                if (this.RequireEmail)
                {
                    _txtEmail.Columns = 30;
                    //AddFieldRow(_lblEmailCaption);
                    AddFieldRow(null, _txtEmail);
                }

                if (this.RequirePassword)
                {
                    //AddFieldRow(_lblPasswordCaption);
                    AddFieldRow(null, new LiteralControl("("), _lblPasswordMinLengthCaption, new LiteralControl(")"), _txtPassword);
                    _lblPasswordMinLengthCaption.Attributes.Add("class", "passwordlenNotification");

                    //AddFieldRow(_lblConfirmPassword);
                    AddFieldRow(null, _txtConfirmPassword);
                }

                if (this.RequireOkToEmail)
                {
                    AddFieldRow(_lblOkToEmailCaption, true, new LiteralControl("<br />"), _rbOkToEmailYesOption, new LiteralControl("&nbsp;"), _rbOkToEmailNoOption);
                    _lblSubscriptionOptionCaption.Attributes.Add("class", "yesnoemialnotification");
                    //AddFieldRow(null, new LiteralControl("&nbsp;"), _lblSubscriptionOptionCaption);
                    _lblSubscriptionOptionCaption.Attributes.Add("class", "yesnoemialnotification");
                }

                if (this.RequireOver13)
                {
                    AddFieldRow(_lblOver13YearsOldCaption, true, _chkOver13YearsOld);
                }

                if (this.RequireSaveCCDetails)
                {
                    AddFieldRow(_lblSaveCCDetails);
                    AddFieldRow(null, _chkSaveCCDetails);
                }

                if (this.RequireCaptcha)
                {
                    AddFieldRow(_lblCaptchaCaption);
                    AddFieldRow(null, _txtCaptcha, new LiteralControl("<br />"), _imgCaptcha);
                }
            }
        }

        [Browsable(false)]
        public Customer.BusinessTypes BusinessType
        {
            get
            {
                if (_cboBusinessType.SelectedValue == Customer.BusinessTypes.WholeSale.ToString())
                {
                    return Customer.BusinessTypes.WholeSale;
                }
                else
                {
                    return Customer.BusinessTypes.Retail;
                }
            }
            set
            {
                string selectedValue = Customer.BusinessTypes.Retail.ToString();
                switch (value)
                {
                    case Customer.BusinessTypes.Retail:
                        selectedValue = Customer.BusinessTypes.Retail.ToString();
                        break;
                    case Customer.BusinessTypes.WholeSale:
                        selectedValue = Customer.BusinessTypes.WholeSale.ToString();
                        break;
                }

                _cboBusinessType.SelectedValue = selectedValue;
            }
        }

        private void ToggleTaxNumberVisibilityBasedOnSelectedBusinessType()
        {
            if (this.RequireBusinessType)
            {
                if (Customer.BusinessTypes.WholeSale == this.BusinessType)
                {
                    _rowTaxNUmber.Style["display"] = "";
                    _rowTaxNUmber.Style["display"] = "";
                }
                else
                {
                    _rowTaxNUmber.Style["display"] = "none";
                    _rowTaxNUmber.Style["display"] = "none";
                }
            }
        }

        public override void RenderControl(HtmlTextWriter writer)
        {
            if (!this.DesignMode)
            {
                ToggleTaxNumberVisibilityBasedOnSelectedBusinessType();
            }

            base.RenderControl(writer);
        }

        protected void OnEvaluateNoDuplicateCustomerEmail(object validator, CancelEventArgs e)
        {
            if (null != EvaluateNoDuplicateCustomerEmail)
            {
                EvaluateNoDuplicateCustomerEmail(this, e);
            }
        }

        #endregion
    }
}
